package com.wisezone.workflow.web;

import java.io.Serializable;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.wisezone.common.annotation.WzFunAnnotation;
import com.wisezone.common.annotation.WzWebAnnotation;
import com.wisezone.common.utils.ConstantUtils;
import com.wisezone.workflow.bean.WfActivity;
import com.wisezone.workflow.bean.WfActivityInstUser;
import com.wisezone.workflow.bean.WfActivityInstance;
import com.wisezone.workflow.bean.WfDataDictionary;
import com.wisezone.workflow.bean.WfProcessDefinition;
import com.wisezone.workflow.bean.WfProcessInstance;
import com.wisezone.workflow.bean.form.WfActivityInstUserForm;
import com.wisezone.workflow.bean.form.WfActivityInstanceForm;
import com.wisezone.workflow.bean.search.WfActivityInstanceSearch;
import com.wisezone.workflow.bean.search.WfProcessInstanceSearch;
import com.wisezone.workflow.core.ActivityInstance;
import com.wisezone.workflow.core.BaseProperty;
import com.wisezone.workflow.exception.WfWebException;

/**
 * 活动实例控制类 2010-08-26
 * @author shan
 *
 */
@WzWebAnnotation(moduleName="activityInst")
public class WfActivityInstController extends WfWorkFlowController<WfActivityInstance,
WfActivityInstanceSearch, WfActivityInstanceForm> {

	
	private List<WfProcessDefinition> processDefList;
	
	private List<WfProcessInstance> processInstList;
	
	private List<WfDataDictionary> stateList;
	
	private List<WfActivity> activityList;
	
	private final String APPOINT = "appoint";
	
	
	 /**
	  * 获得列表
	  * @return
	  * @throws Exception
	  */
	 public String list() throws WfWebException{
		 
		 searchEntity = getSearchEntity(true);
		 pagination = flowEngine.getWfActivityInstanceDao().listFormByPage(searchEntity);
		 return LIST;
	 }

	 
	 /**
	  * 进入活动操作
	  * @return
	  * @throws Exception
	  */
	 public String enterOperate() throws WfWebException{
		 
		 form = flowEngine.getWfActivityInstanceDao().loadForm(id);
		 
		 //获得活动列表
		 Long processDefId = form.getActivity().getProcessDefId();
		 activityList = flowEngine.getWfActivityDao().getNotStartList(processDefId);
		 return NEW_EDIT;
	 }
	 
	 /**
	  * 进入活动委任
	  * @return
	  * @throws Exception
	  */
	 public String enterAppoint() throws WfWebException{
		 
		 List<WfActivityInstUserForm> list = flowEngine.getWfActivityInstUserDao().listFormByActivityInstId(id);
		 this.model.put("list", list);
		 return APPOINT;
	 }
	 
	 /**
	  * 进入查看
	  * @return
	  * @throws Exception
	  */
	 public String enterView() throws WfWebException{
		 
		 form = flowEngine.getWfActivityInstanceDao().loadForm(id);
		 
		 //获得活动实例参与人和参与人组
		 String userIds = "";
		 String userGroupIds = "";
		 List<WfActivityInstUser> list = flowEngine.getWfActivityInstUserDao().listByActivityInstId(form.getId());
		 if(list != null){
			 for(Iterator<WfActivityInstUser> it = list.iterator(); it.hasNext();){
				 WfActivityInstUser activityInstUser = it.next();
				 if(activityInstUser.getUserId() != null){
					 userIds += "," + activityInstUser.getUserId();
				 }else if(activityInstUser.getUserGroupId() != null){
					 userGroupIds += "," + activityInstUser.getUserGroupId();
				 }
			 }
			 if(!userIds.equals("")){
				 userIds = userIds.substring(1);
				 this.model.put("userIds", userIds);
			 }
			 if(!userGroupIds.equals("")){
				 userGroupIds = userGroupIds.substring(1);
				 this.model.put("userGroupIds", userGroupIds);
			 }
		 }
		 return VIEW;
	 }
	 
	 /**
	  * 开始活动
	  * @return
	  * @throws Exception
	  */
	 public String start() throws WfWebException{

		 ActivityInstance ai = flowEngine.loadActivityInstance(id);
		 ai.start();
		 flowEngine.submit(ai);
		 
		 return getRedirect(LIST);
	 }
	 
	 /**
	  * 完成活动
	  * @return
	  * @throws Exception
	  */
	 public String complate() throws WfWebException{
		 
		 searchEntity = getSearchEntity();
		 ActivityInstance ai = flowEngine.loadActivityInstance(id);
		 Long actor = getUserId();
		 Long nextActivityId = searchEntity.getNextActivityId();
		 Map<String,Object> activityMap = new HashMap<String, Object>();
		 activityMap.put(BaseProperty.NEXT_ACTIVITY_ID, nextActivityId);
		 ai.complete(actor, activityMap,null, "");
		 flowEngine.submit(ai);
		 
		 return getRedirect(LIST);
	 }
	 
	 /**
	  * 活动委任
	  * @return
	  * @throws WfWebException
	  */
	 public String appoint() throws WfWebException{
		 
		 Collection<Serializable> ids = getIdList();
		 for(Serializable id : ids){
			 
		 }
		 return getRedirect(LIST);
	 }
	 
	 /**
	  * 取消活动委任
	  * @return
	  * @throws WfWebException
	  */
	 public String cancelAppoint() throws WfWebException{
		 
		 return getRedirect(LIST);
	 }
	 
	 /**
	  * 修改
	  * @return
	  * @throws Exception
	  */
	 public String update() throws WfWebException{
		 
		 entity = getEntity();
		 return getRedirect(LIST);
	 }
	 

	
	/**
	 * 活动实例结束
	 * @return
	 * @throws Exception
	 */
	public String end() throws WfWebException{
		
		ActivityInstance ai = flowEngine.loadActivityInstance(id);
		Long actor = getUserId();
		ai.complete(actor, "");
		flowEngine.submit(ai);
		return getRedirect(LIST);
	}
	 
	 
	 /**
	  * 删除
	  * @return
	  * @throws Exception
	  */
	 public String delete() throws WfWebException{
		 
		 Collection<Serializable> idList = getIdList();
		 if(idList != null){
			flowEngine.getWfActivityInstanceDao().deleteAllById(idList);
		 }
		 return getRedirect(LIST);
	 }
	 
	 

	@WzFunAnnotation(act = ConstantUtils.EXEC_AJAX)
	public List<WfProcessDefinition> getProcessDefList() {
		
		processDefList = flowEngine.getWfProcessDefinitionDao().getMaxVerList();
		return processDefList;
	}

	@WzFunAnnotation(act = ConstantUtils.EXEC_AJAX)
	public List<WfProcessInstance> getProcessInstList() {
		
		 WfProcessInstanceSearch processInstSearch = new WfProcessInstanceSearch();
		 searchEntity = getSearchEntity();
		 processInstSearch.setProcessDefId(searchEntity.getProcessDefId());
		 processInstList = flowEngine.getWfProcessInstanceDao().listAll(processInstSearch);
		return processInstList;
	}

	@WzFunAnnotation(act = ConstantUtils.EXEC_AJAX)
	public List<WfDataDictionary> getStateList() {
		
		stateList = flowEngine.getWfDataDictionaryDao().listByParentId(5L);
		return stateList;
	}

	@WzFunAnnotation(act = ConstantUtils.EXEC_AJAX)
	public List<WfActivity> getActivityList() {
		return activityList;
	}
	

	 
	

}
